package com.yyfax.ph.common.share;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.ph.R;
import com.yyfax.ph.common.share.ShareAgent.ShareAction;
import com.yyfax.ph.home.recommendloan.RecommendPermissionModel;

import java.lang.ref.WeakReference;
import java.util.List;

public class ShareDialog extends Dialog implements ShareAgent.MsgSender {


	private static final String qqPackage = "com.tencent.mobileqq";
	private static final String wxPackage = "com.tencent.mm";
	private static final String wbPackage = "com.sina.weibo";

	private WeakReference<Activity> mAtyReference;
	private ShareAgent mShareAction;

	public ShareDialog(Activity aty, RecommendPermissionModel.ShareBean bean) {
		super(aty);
		mAtyReference = new WeakReference<>(aty);
		mShareAction = new ShareAgent(aty, bean.url, bean.title, bean.content, this);
	}

	public void onDestroy() {
		mShareAction.onDestroy();
	}

	@Override
	public void sendMsg(String title, String content, String url) {
		Activity aty = mAtyReference.get();
		if (aty != null && !aty.isFinishing()) {
			Uri uri = Uri.parse("smsto:");
			Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
			intent.putExtra("sms_body", title + content + url);
			aty.startActivity(intent);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_share);
		ButterKnife.bind(this);
		initWindowParams();
	}

	private void initWindowParams() {
		Window window = getWindow();
		if (window != null) {
			window.setBackgroundDrawableResource(android.R.color.transparent);
			window.setGravity(Gravity.BOTTOM);
			window.setWindowAnimations(R.style.AnimBottom);
			window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}
	}

	@OnClick({R.id.we_chat_friend, R.id.we_chat_circle, R.id.sim_message, R.id.cancel_tv})
	public void onClick(View v) {
		Activity aty = mAtyReference.get();
		if (aty == null || aty.isFinishing()) {
			return;
		}
		switch (v.getId()) {
			case R.id.we_chat_friend:
				if (!isAvailable(aty, wxPackage)) {
					Toast.makeText(aty, "应用未安装!", Toast.LENGTH_SHORT).show();
					return;
				}
				if (null != mShareAction) {
					mShareAction.sharePlantForm(ShareAction.WX_FRIEND, R.drawable.ic_launcher);
				}
				dismiss();
				break;
			case R.id.we_chat_circle:
				if (!isAvailable(aty, wxPackage)) {
					Toast.makeText(aty, "应用未安装!", Toast.LENGTH_SHORT).show();
					return;
				}
				if (null != mShareAction) {
					mShareAction.sharePlantForm(ShareAction.WX_CIRCLE, R.drawable.ic_launcher);
				}
				dismiss();
				break;
/*			case R.id.qq:
				if (!isAvailable(aty, qqPackage)) {
					Toast.makeText(aty, "应用未安装!", Toast.LENGTH_SHORT).show();
					return;
				}
				if (null != mShareAction) {
					mShareAction.sharePlantForm(ShareAction.QQ_FRIEND);
				}
				dismiss();
				break;*/
			case R.id.sim_message:
				if (null != mShareAction) {
					mShareAction.sharePlantForm(ShareAction.SEND_FRIEND, R.drawable.ic_launcher);
				}
				dismiss();
				break;
			case R.id.cancel_tv:
				dismiss();
				break;
			default:
				break;
		}
	}

	private boolean isAvailable(Context ctx, String packageName) {
		final PackageManager pkgMgr = ctx.getPackageManager();
		List<PackageInfo> info = pkgMgr.getInstalledPackages(0);
		for (int i = 0; i < info.size(); i++) {
			if (info.get(i).packageName.equalsIgnoreCase(packageName))
				return true;
		}
		return false;
	}
}
